﻿using App.Dal;
using App.Model;
using App.Untiys;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Win.Dal;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace App.Desk.Sys.Role
{
    public partial class FrmEdit : UIEditForm
    {
        private bool CheckDropEmpty(UIIntegerUpDown uIIntegerUpDown, string retMsg)
        {
            if (uIIntegerUpDown.Value <= 0)
            {
                UIMessageBox.ShowWarning(retMsg);
                return false;
            }
            return true;
        }

        private SysRole postData;

        public SysRole PostData
        {
            get
            {
                if (postData == null)
                {
                    postData = new SysRole();
                }
                postData.Id = txtId.Text.Trim().ToInt();
                postData.RoleName = txtRoleName.Text.Trim();
                postData.Disable = txtDisable.Active ? 1 : 0;
                return postData;
            }
            set
            {
                postData = value;
                txtId.Text = value.Id.ToStringEx();
                txtRoleName.Text = value.RoleName;
                //depart
                txtDisable.Active = value.Disable == 1;
            }
        }
        public FrmEdit(int id = 0)
        {
            InitializeComponent();
            BindAuthoritys();
            if (id <= 0)
                Text = "新增";
            else
            {
                Text = "修改";
                PostData = SysRoleServices.Info(new SysRole { Id = id });
               
            }
        }

        private void FrmEdit_Load(object sender, EventArgs e)
        {
            SetAuthority(PostData.Authority);
        }

        /// <summary>
        /// 绑定权限选择框
        /// </summary>
        public void BindAuthoritys()
        {
            var menus = ConfigServices.GetNoDisableMenus();
            var mains = menus.Where(m => m.Parent == 0).OrderBy(m => m.Id).ToList();
            UICheckBox uICheck = new UICheckBox();
            uICheck.Name = "强制采样";
            uICheck.Text = "强制采样";
            flowAuthority.Add(uICheck);
            mains.ForEach(mitem =>
            {
                var childrens = menus.Where(m => m.Parent == mitem.Id).OrderBy(m => m.Id).ToList();
                childrens.ForEach(res =>
                {
                    uICheck = new UICheckBox();
                    uICheck.Name = res.Name;
                    uICheck.Text = res.Name;
                    flowAuthority.Add(uICheck);
                });
            });
        }

        /// <summary>
        /// 根据用户本身权限 勾选权限框
        /// </summary>
        /// <param name="authority"></param>
        public void SetAuthority(string authority)
        {
            List<string> authoritys = new List<string>();
            if (!string.IsNullOrEmpty(authority))
                authoritys = authority.ToSplitArrayEx('|');
            foreach (var control in flowAuthority.GetAllControls())
                if (control is UICheckBox)
                {
                    if (authoritys.Contains(((UICheckBox)control).Text))
                    {
                        ((UICheckBox)control).Checked = true;
                    }
                }
        }

        /// <summary>
        /// 将选择的权限 转为字符串
        /// </summary>
        /// <returns></returns>
        public string GetAuthority()
        {
            List<string> authoritys = new List<string>();
            foreach (var control in flowAuthority.GetAllControls())
                if (control is UICheckBox)
                    if (((UICheckBox)control).Checked)
                        authoritys.Add(((UICheckBox)control).Text);
            if (authoritys.IsNotNull())
                return authoritys.ToSplitStringEx("|");
            return "";
        }

        private void FrmEdit_ButtonOkClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtRoleName.Text.Trim()))
            {
                txtRoleName.Focus();
                UIMessageTip.ShowWarning("请输入名称！"); return;
            }
            if (SysRoleServices.Isrepeat(PostData.Id, txtRoleName.Text.Trim()))
            {
                txtRoleName.Focus();
                UIMessageTip.ShowWarning("角色名称重复，请重新输入！"); return;
            }
            if (string.IsNullOrEmpty(txtRoleName.Text.Trim()))
            {
                txtRoleName.Focus();
                UIMessageTip.ShowWarning("请输入名称！"); return;
            }
            PostData.AddUser = Config.CurrentUser.Id;
            PostData.Authority = GetAuthority();
            if (SysRoleServices.Edit(PostData))
            {
                UIMessageBox.ShowSuccess("保存成功！");
                this.Close();
            }
            else
                UIMessageBox.ShowError("保存失败！");
        }
       
    }
}
